<?php
/**
 * Customar Order actions
 */
class customarOrderActions extends myFrontModuleActions {
    public function executeAddToCartWidget(dmWebRequest $request) {

        if($request->isMethod('post')) {
            $this->getUser()->setAttribute('shopping_redirect_url',$request->getReferer());
            $qty = (int)$request->getPostParameter('qty');
            $color = $request->getPostParameter('color');
            $size = $request->getPostParameter('size');
            $product_id = $request->getPostParameter('product_id');
            $product = ProductTable::getInstance()->find($product_id);
            if(!$product || !is_int($qty))
                $this->redirectBack();

            $item = new sfShoppingCartItem('Product', $product_id);
            $item->setQuantity($qty);
            $item->setPrice(($product->getPrice()));
            $item->setColor($color);
            $item->setSize($size);
            $shopping_cart = $this->getUser()->getShoppingCart();
            $shopping_cart->addItem($item);
        }
    }
    public function executeUpdate(dmWebRequest $request) {

        $updateItems = $request->getPostParameter('qty');
        $color = $request->getPostParameter('color');
        $size = $request->getPostParameter('size');
        $product_id = $request->getPostParameter('product_id');
//        print_r($updateItems);
//        print_r($color);
//        print_r($size);
//        print_r($product_id);
        $shopping_cart = $this->getUser()->getShoppingCart();
        foreach($updateItems as $updateItem_id=>$updateValue) {
            $item = $shopping_cart->getItem('Product', $updateItem_id,$size[$updateItem_id],$color[$updateItem_id]);
            if(!$item)
                continue;
            $product =ProductTable::getInstance()->find($item->getId());
            $item->setQuantity((int)$updateValue);
            $item->setPrice(($product->getPrice()));

        }
//        die('milon');
        $this->redirectBack();
    }
    public function executeDelete(dmWebRequest $request) {

        if ($request->hasParameter('id') && $request->hasParameter('size') && $request->hasParameter('color')) {
            $shopping_cart = $this->getUser()->getShoppingCart();

            $shopping_cart->deleteItem('Product', $request->getParameter('id'), $request->getParameter('size'),$request->getParameter('color') );
        }
        $this->redirectBack();
    }

    public function executeAddCustomerOrder(dmWebRequest $request) {

        $shopping_cart = $this->getUser()->getShoppingCart();
        if($shopping_cart->getNbItems()) {

            $userPaymentInfo = CustomarOrderTable::getInstance()->addCustomerOrder(
                    $this->getUser(),
                    $shopping_cart,
                    $request->getRemoteAddress()
            );
            if($userPaymentInfo) {
                $this->getUser()->setAttribute('added_order', $userPaymentInfo);
                $this->getUser()->getShoppingCart()->clear();
                if($this->getUser()->hasGroup('Prime') ) {
                    $this->getService('mail')->setTemplate('dm_user_order_confirm_prime')
                            ->addValues(array(
                            'orderid'      => $userPaymentInfo->getId(),
                            'email'         => $userPaymentInfo->DmUser->getEmail()
                            ))
                            ->send();
                }else  if($this->getUser()->hasGroup('Premier') ) {
                    $this->getService('mail')->setTemplate('dm_user_order_confirm_pre')
                            ->addValues(array(
                            'orderid'      => $userPaymentInfo->getId(),
                            'email'         => $userPaymentInfo->DmUser->getEmail()
                            ))
                            ->send();
                }
                $this->redirect($this->getHelper()->link('customarOrder/paymentSuccess')->getHref());
            }else {
                $this->redirectBack();
            }


        }
        $this->redirectBack();
//        $this->redirect('@homepage');
    }
    public function executeShippingMethodWidget(dmWebRequest $request) {
        $form = new ProfileShippingForm();

        if($request->isMethod('post') && $request->hasParameter($form->getName())) {
            $form->bind($request->getParameter($form->getName()));
            if ($form->isValid()) {
                $values = $form->getValues();

                $profile = $this->getUser()->getDmUser()->getProfile();
                $profile->setShippingAddress1($values['shipping_address1']);
                $profile->setShippingAddress2($values['shipping_address2']);
                $profile->setShippingCity($values['shipping_city']);
                $profile->setShippingPostCode($values['shipping_post_code']);
                $profile->setShippingCountry($values['shipping_country']);
                $profile->save();
                $this->redirectBack();
            }
        }
        $this->forms['ProfileShipping'] = $form;
    }
}
